/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.twiddling.core.io.text;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author <a href="mailto:howard.gao@gmail.com">Howard Gao</a> 
 *
 */
public class TextTable {
	
	private int width;
	private TableSchema schema;
	private String title;
	private String subTitle;
	private ArrayList<ArrayList<Cell>> rows = new ArrayList<ArrayList<Cell>>(); 

	public TextTable(TableSchema tableSchema) {
		schema = tableSchema;
		width = 80;
	}

	public void addColumns(String[] strings) {
		for (String name : strings) {
			schema.addColumn(name);
		}
	}

	public void addRow(String[] values) {
		ArrayList<Cell> row = new ArrayList<Cell>();
		for (String val : values) {
			row.add(new Cell(val));
		}
		rows.add(row);
	}
	
	/**
	 * format the table into lines of output
	 * 
	 * 1. format the title and subtitle
	 * 2. format the header
	 * 3. format the body
	 * @return
	 */
	public List<String> format() {
		ArrayList<String> allLines = new ArrayList<String>();
		formatTitles(allLines);
		formatHeader(allLines);
		formatBody(allLines);
		return allLines;
	}

	private void formatTitles(ArrayList<String> allLines) {
		schema.formatTitles(allLines, this);
	}

	private void formatBody(ArrayList<String> allLines) {
		schema.formatBody(allLines, this);
	}

	private void formatHeader(ArrayList<String> allLines) {
		schema.formatHeader(allLines, this);
	}

	public void print(PrintWriter out) {
		List<String> lines = format();
		int n = lines.size();
		for (int i = 0; i < n; i++) {
			String ln = lines.get(i);
			out.println(ln);
		}
		out.flush();
	}
	
	public int getWidth() {
		return width;
	}

	public String getTitle() {
		return title;
	}

	public String getSubTitle() {
		return subTitle;
	}

	public void setWidth(int totWid) {
		width = totWid;
	}
	
	public ArrayList<ArrayList<Cell>> getRows() {
		return rows;
	}

	public String getHLine() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < width; i++) {
			sb.append("-");
		}
		return sb.toString();
	}

	public void setColumnWidth(String cname, int ith) {
		schema.setColumnWidth(cname, ith, this);
	}

	public TableSchema getSchema() {
		return schema;
	}
}
